import itchat,re
import MySQLdb,time
import urllib
import urllib2
from wxpy import *
reload(sys)
sys.setdefaultencoding("utf8")
itchat.auto_login()
friends = itchat.get_friends(update = True)[0:]
for i in range (0,len(friends)):
    friend = friends[i]
    img = itchat.get_head_img(userName=friend["UserName"])
    for im in img:
         conn = MySQLdb.connect(host='localhost', user='root', passwd='', charset="utf8",use_unicode=False)
         cur = conn.cursor()
         try:
             A = cur.execute("INSERT INTO weixin.img SET data='%s'" % MySQLdb.Binary(im))
             conn.commit()
         except Exception as e:
             print(e)
             conn.rollback()


